package com.wsjj.yjh.window;

import com.wsjj.yjh.WaterSensor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class WindowAggregateAndProcessDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);
        env.enableCheckpointing(2000, CheckpointingMode.EXACTLY_ONCE);

        DataStreamSource<String> source = env.fromSource(KafkaSource.<String>builder()
                .setBootstrapServers("hadoop102:9092,hadoop103:9092")
                .setTopics("topic_sink")
                .setGroupId("yjh")
                .setStartingOffsets(OffsetsInitializer.latest())
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build(), WatermarkStrategy.noWatermarks(), "kafkaSource");
        SingleOutputStreamOperator<WaterSensor> map = source.map(value -> {
            String[] split = value.split(",");
            WaterSensor waterSensor = new WaterSensor(split[0], Long.valueOf(split[1]), Integer.valueOf(split[2]));
            return waterSensor;
        });

        KeyedStream<WaterSensor, String> waterSensorStringKeyedStream = map.keyBy(WaterSensor::getId);

        WindowedStream<WaterSensor, String, TimeWindow> window = waterSensorStringKeyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(20)));


        SingleOutputStreamOperator<String> aggregate = window.aggregate(new MyAgg(), new Mypocess());

        aggregate.print();


        env.execute();
    }

    private static class MyAgg implements AggregateFunction<WaterSensor,Integer,String> {
        @Override
        public Integer createAccumulator() {
            return 0;
        }

        @Override
        public Integer add(WaterSensor value, Integer accumulator) {
            return value.getVc() + accumulator;
        }

        @Override
        public String getResult(Integer accumulator) {

            return accumulator.toString();
        }

        @Override
        public Integer merge(Integer a, Integer b) {
            return null;
        }
    }

    private static class Mypocess implements WindowFunction<String,String,String,TimeWindow> {
        @Override
        public void apply(String s, TimeWindow window, Iterable<String> input, Collector<String> out) throws Exception {
            long start = window.getStart();
            long end = window.getEnd();
            long count = input.spliterator().estimateSize();
            String startWindow = DateFormatUtils.format(start, "yyyy-MM-dd HH:mm:ss.SSS");
            String endWindow = DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss.SSS");
            out.collect("窗口的大小为["+startWindow+","+endWindow+"),个数为："+count+"数据为"+input.toString());

        }
    }
}
